﻿//用于保存预警发布窗体
var win_append_reaction = null;

//更新应急反应信息
function RefreshEmergencyReaction()
{
	//创建请求对象
	Ext.Ajax.request
        (
            {
            	url: Common.MakeControllerUrl('EmergencyReaction', 'LastestEmergencyReaction'),
            	method: HttpMethod.Get,
            	callback: function (options, success, response)
            	{
            		//隐藏应急反应
            		Ext.fly('emergency_reaction_statusbar').hide();

            		//判定是否发生错误
            		if (!success && options && options.failure)
            		{
            			//直接返回
            			return;
            		}

            		//判定是否有反馈对象
            		if (!response)
            		{
            			//直接返回
            			return;
            		}

            		//用于保存反馈对象
            		var responseData = {};

            		//解析反馈对象
            		try
            		{
            			//解析反馈数据
            			responseData = Ext.JSON.decode(response.responseText, true);
            		}
            		catch (error)
            		{
            			//直接返回
            			return;
            		}

            		//判定是否称成功
            		if (responseData.success && responseData.data)
            		{
            			//获得数据
            			var data = responseData.data;

            			//显示应急反应
            			Ext.fly('emergency_reaction_statusbar').show();

            			//判定是否有图片
            			if (data.ImageUrl)
            			{
            				//更新图片
            				// Ext.fly('emergency_image').set({ 'src': data.ImageUrl });
            			}

            			//获得控件
            			if (data.Content)
            			{
            				Ext.fly('emergency_text').update(data.Content);
            			}
            		}
            	}
            }
        );
}

//添加天气预警
function AppendEmergencyReaction(callback)
{
	//判定窗体是否创建
	if (!win_append_reaction)
	{
		//创建天气预警存储
		var store_master_weather_warnings = Ext.create
        (
            'Ext.data.Store',
            {
            	fields: ['Name', 'PinYin', 'Value'],
            	data: master_weatherwarning
            }
        );

		//创建响应等级存储
		var store_master_reaction_level = Ext.create
        (
            'Ext.data.Store',
            {
            	fields: ['Name', 'PinYin'],
            	data: master_reaction_level
            }
        );

		//创建预警等级存储
		var store_master_alarm_level = Ext.create
        (
            'Ext.data.Store',
            {
            	fields: ['Name', 'PinYin'],
            	data: master_alarm_level
            }
        );

		//创建窗体
		win_append_reaction = new Ext.Window
        (
            {
            	title: '发布应急反应',
            	maximizable: false,
            	maximized: false,
            	width: 360,
            	height: 240,
            	autoScroll: false,
            	bodyBorder: false,
            	draggable: true,
            	isTopContainer: true,
            	modal: true,
            	resizable: false,
            	bodyPadding: 0,
            	closeAction: 'hide',
            	closable: false,
            	layout: 'fit',
            	fieldDefaults:
                {
                	labelAlign: 'left',
                	labelWidth: 72,
                	anchor: '100%'
                },
            	items:
                [
                    {
                    	xtype: 'form',
                    	method: 'POST',
                    	waitMsgTarget: true,
                    	waitTitle: Common.ApplicationName,
                    	frame: false,
                    	bodyPadding: 12,
                    	bodyStyle: 'background-color:White;margin:0;',
                    	bodyBorder: false,
                    	border: 0,
                    	autoScroll: true,
                    	defaultType: 'textfield',
                    	autoScroll: false,
                    	fieldDefaults:
                        {
                        	labelAlign: 'left',
                        	labelWidth: 72,
                        	anchor: '100%',
                        	msgTarget: 'qtip',
                        	autoFitErrors: true,
                        	disabledCls: ''
                        },
                    	items:
                        [
                            {
                            	xtype: 'hiddenfield',
                            	itemId: 'txtWeatherWarningUrl',
                            	name: 'WeatherWarningUrl',
                            	allowBlank: true
                            },
                            {
                            	xtype: 'combobox',
                            	name: 'WeatherWarning',
                            	itemId: 'cboWeatherWarning',
                            	fieldLabel: '天气预警',
                            	emptyText: '天气预警',
                            	store: store_master_weather_warnings,
                            	displayField: 'PinYin',
                            	valueField: 'Name',
                            	queryMode: 'local',
                            	hideTrigger: false,
                            	forceSelection: true,
                            	typeAhead: true,
                            	autocomplete: true,
                            	allowBlank: false,
                            	listeners:
                                {
                                	select: function (field, records, eOpts)
                                	{
                                		//判断是否有记录
                                		if (records.length > 0)
                                		{
                                			//设置结果
                                			field.up('form').down('#txtWeatherWarningUrl').setValue(records[0].data.Value);
                                		}
                                	}
                                }
                            },
                            {
                            	xtype: 'combobox',
                            	name: 'ReactionLevel',
                            	itemId: 'cboReactionLevel',
                            	fieldLabel: '响应等级',
                            	emptyText: '应急反应等级',
                            	store: store_master_reaction_level,
                            	displayField: 'PinYin',
                            	valueField: 'Name',
                            	queryMode: 'local',
                            	hideTrigger: false,
                            	forceSelection: true,
                            	typeAhead: true,
                            	autocomplete: true,
                            	allowBlank: false
                            },
							{
								xtype: 'combobox',
								name: 'AlarmLevel',
								itemId: 'cboAlarmLevel',
								fieldLabel: '预警等级',
								emptyText: '公路服务与保畅预警等级',
								store: store_master_alarm_level,
								displayField: 'PinYin',
								valueField: 'Name',
								queryMode: 'local',
								hideTrigger: false,
								forceSelection: true,
								typeAhead: true,
								autocomplete: true,
								allowBlank: true
							},
                            {
                            	xtype: 'panel',
                            	border: 0,
                            	layout:
                                {
                                	type: 'hbox'
                                },
                            	items:
                                [
                                    {
                                    	xtype: 'datefield',
                                    	itemId: 'txtPostDate',
                                    	fieldLabel: '发布时间',
                                    	emptyText: '发布日期',
                                    	allowBlank: false,
                                    	value: new Date(),
                                    	format: 'Y/m/d',
                                    	width: 198
                                    },
                                    {
                                    	xtype: 'numberfield',
                                    	padding: '0 0 0 3',
                                    	itemId: 'txtHour',
                                    	emptyText: '时',
                                    	allowBlank: false,
                                    	value: 0,
                                    	width: 42,
                                    	minValue: 0,
                                    	maxValue: 23,
                                    	decimalPrecision: 0
                                    },
                                    {
                                    	xtype: 'displayfield',
                                    	value: '&nbsp;&nbsp;时'
                                    },
                                    {
                                    	xtype: 'numberfield',
                                    	padding: '0 0 0 3',
                                    	itemId: 'txtMinute',
                                    	emptyText: '分',
                                    	allowBlank: false,
                                    	width: 42,
                                    	value: 0,
                                    	minValue: 0,
                                    	maxValue: 59,
                                    	decimalPrecision: 0
                                    },
                                    {
                                    	xtype: 'displayfield',
                                    	value: '&nbsp;&nbsp;分'
                                    }
                                 ]
                            },
                            {
                            	xtype: 'panel',
                            	border: 0,
                            	layout:
                                {
                                	type: 'hbox'
                                },
                            	items:
                                [
                                    {
                                    	xtype: 'numberfield',
                                    	name: 'ExpireDuration',
                                    	itemId: 'txtExpireDuration',
                                    	fieldLabel: '有效期限',
                                    	emptyText: '有效期限',
                                    	allowBlank: false,
                                    	width: 290,
                                    	minValue: 0,
                                    	value: 24,
                                    	decimalPrecision: 1
                                    },
                                    {
                                    	xtype: 'displayfield',
                                    	value: '&nbsp;&nbsp;小时'
                                    }
                                 ]
                            }
                        ],
                    	buttons:
                        [
                            {
                            	text: '发布',
                            	height: 26,
                            	margin: 3,
                            	handler: function ()
                            	{
                            		//获得表单对象
                            		var form = this.up('form');

                            		//获得提交表单
                            		var formBase = form.getForm();

                            		//检查数据是否合法
                            		if (formBase.isValid())
                            		{
                            			//获得发布日期
                            			var postDate = form.down('#txtPostDate').getValue();

                            			///获得发布小时
                            			var postHour = form.down('#txtHour').getValue();

                            			//添加小时
                            			postDate = Ext.Date.add(postDate, Ext.Date.HOUR, postHour);

                            			///获得发布分钟
                            			var postMinute = form.down('#txtMinute').getValue();

                            			//添加分钟
                            			postDate = Ext.Date.add(postDate, Ext.Date.MINUTE, postMinute);

                            			//提交数据
                            			formBase.submit
                                        (
                                            {
                                            	url: Common.MakeControllerUrl('EmergencyReaction', 'Create'),
                                            	method: HttpMethod.Post,
                                            	params: { PostDate: postDate },
                                            	scope: this.up('window'),
                                            	submitEmptyText: false,
                                            	waitTitle: '请稍候',
                                            	waitMsg: '正在保存数据...',
                                            	success: function (form, action)
                                            	{
                                            		//处理提交错误
                                            		if (Common.HandleFormError(form, action))
                                            		{
                                            			//重置数据
                                            			form.reset();
                                            			//隐藏窗体
                                            			this.hide();

                                            			//更新应急反应信息
                                            			RefreshEmergencyReaction();

                                            			//回调
                                            			if (callback)
                                            			{
                                            				//回调
                                            				callback();
                                            			}
                                            		}
                                            	},
                                            	failure: Common.HandleFormFailure
                                            }
                                        );
                            		}
                            	}
                            },
                            {
                            	text: '取消',
                            	height: 26,
                            	margin: 3,
                            	handler: function ()
                            	{
                            		//获得表单对象
                            		var form = this.up('form').getForm();
                            		//重置数据
                            		form.reset();
                            		//隐藏窗体
                            		this.up('window').hide();
                            	}
                            }
                        ]
                    }
                ]
            }
        );
	}

	//显示窗体
	win_append_reaction.show();
}

//挂接环境准备完毕事件
Ext.onReady
(
    function ()
    {
    	//刷新数据
    	RefreshEmergencyReaction();

    	//注册定时器，5分钟刷新一次
    	window.setInterval('RefreshEmergencyReaction()', 300000);
    }
);